import 'package:flutter/material.dart';

class LifecycleAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LifecycleAppPageState('构造函数');
  }
}

class _LifecycleAppPageState extends State<LifecycleAppPage>
    with WidgetsBindingObserver {
  String str;

  int count = 0;

  _LifecycleAppPageState(this.str);

  @override
  void initState() {
    print(str);
    print('initState');
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void didChangeDependencies() {
    print('didChangeDependencies');
    super.didChangeDependencies();
  }

  @override
  void reassemble() {
    print('热重载-reassemble');
    super.reassemble();
  }

  @override
  void didUpdateWidget(LifecycleAppPage oldWidget) {
    print('热重载-didUpdateWidget');
    super.didUpdateWidget(oldWidget);
  }

  @override
  void deactivate() {
    print('返回-deactivate');
    var bool = ModalRoute.of(context).isCurrent;
    if (bool) {
      // 监听到页面返回，发请求刷新页面操作
      print('返回NewView页面');
    }
    super.deactivate();
  }

  @override
  void dispose() {
    print('返回-dispose');
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // switch (state) {
    //   case AppLifecycleState.inactive:
    //     print('AppLifecycleState.inactive');
    //     break;
    //   case AppLifecycleState.paused:
    //     print('AppLifecycleState.paused');
    //     break;
    //   case AppLifecycleState.resumed:
    //     print('AppLifecycleState.resumed');
    //     break;
    //   case AppLifecycleState.suspending:
    //     print('AppLifecycleState.suspending');
    //     break;
    // }

    super.didChangeAppLifecycleState(state);
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    return Scaffold(
      appBar: AppBar(
        title: Text('lifecycle 学习'),
        centerTitle: true,
      ),
      body: OrientationBuilder(
        builder: (context, orientation) {
          print(orientation);
          return Center(
            child: Row(
              key: UniqueKey(),
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: [
                    SizedBox(
                      width: 50,
                      height: 100,
                      child: TextButton(
                          onPressed: () {
                            Future.delayed(Duration(seconds: 4)).then((value) {
                              print('我还在呢');
                              setState(() {
                                count++;
                                print(count.toString());
                              });
                            });
                          },
                          child: Text('点击')),
                    ),
                    Box(),
                    Text(
                      '当前计数值：$count',
                      style: TextStyle(
                          color: orientation == Orientation.portrait
                              ? Colors.blue
                              : Colors.red),
                    ),
                    Text(
                      '当前计数值：$count',
                      style: TextStyle(
                          color: orientation == Orientation.portrait
                              ? Colors.blue
                              : Colors.red),
                    ),
                  ],
                ),
                Column(
                  children: [
                    Box(),
                    Text(
                      '当前计数值：$count',
                      style: TextStyle(
                          color: orientation == Orientation.portrait
                              ? Colors.blue
                              : Colors.red),
                    ),
                    Text(
                      '当前计数值：$count',
                      style: TextStyle(
                          color: orientation == Orientation.portrait
                              ? Colors.blue
                              : Colors.red),
                    ),
                  ],
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          FloatingActionButton(
              heroTag: 'num',
              key: UniqueKey(),
              child: Text('num++'),
              onPressed: () {
                setState(() {
                  count++;
                });
              }),
          FloatingActionButton(
              heroTag: 'next',
              key: UniqueKey(),
              child: Text('next'),
              onPressed: () {
                // Navigator.pushNamed(context, '/flutterlife');
                // Navigator.of(context).pushNamed('/repaint_boundary');
                Future future = Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => LifecycleAppPage()));
              }),
          FloatingActionButton(
              heroTag: 'pop',
              key: UniqueKey(),
              child: Text('pop'),
              onPressed: () {
                // Navigator.of(context).pop('111');
                Navigator.pop(context);
              }),
        ],
      ),
    );
  }

  void add() {}
}

class LifecyclePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LifecycleAppPage(),
    );
  }
}

class Box extends StatefulWidget {
  @override
  _BoxState createState() => _BoxState();
}

class _BoxState extends State<Box> {
  @override
  void didChangeDependencies() {
    print('Box-didChangeDependencies');
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(covariant Box oldWidget) {
    print('Box-didUpdateWidget');
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text('Box')),
    );
  }
}
